/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} left
 * @param {number} right
 * @return {ListNode}
 */
 var reverseBetween = function(head, left, right) {
    if(head.next==null || left==right){
        return head;
    }
    let newhead=new ListNode();
    newhead.next=head;
    let node = newhead;
    let i;
    for(i=0;i<left-1;i++){
        node=node.next;
    }
    let pn=node;
    for(;i<right;i++){
        let p=node.next;
        node.next=p.next;
        p.next=pn.next;
        pn.next=p;
        console.log(node,'---');
        console.log(head);
    }
    return head;
};
 /* var reverseBetween = function(head, left, right) {
    if(head.next==null || left==right){
        return head;
    }
    let node = head;
    let newlist = new ListNode();
    let i;
    for(i=1;i<left-1;i++){
        node=node.next;
    }
    let pn=node;
    let pnn=node.next;
    for(;i<right;i++){
        let newnode=newlist;
        let p=node.next;
        node.next=p.next;
        p.next=newnode.next;
        newlist.next=p;
        newnode=newlist;
    }
    if(left==1){
        pnn.next=head;
        head=newlist.next;
    }
    else{
        pnn.next=pn.next;
        pn.next=newlist.next;
    }
    return head;
}; */